<template>
  <div class="sysadmin-container">
    <div class="sysadmin-header">
      <el-input v-model="keywords" size="small" placeholder="通过用户名搜索用户..." prefix-icon="el-icon-search" style="width: 400px; margin-right: 10px"></el-input>
      <el-button size="small" type="primary" icon="el-icon-search" @click="doSearch">搜索</el-button>
    </div>
    <div class="admin-container">
      <el-card class="admin-card" v-for="admin in admins" :key="admin.id">
        <div slot="header" class="clearfix">
          <span>{{ admin.name }}</span>
          <el-button style="float: right; padding: 3px 0; color: red" type="text" icon="el-icon-delete" @click="deleteAdmin(admin)"></el-button>
        </div>
        <div class="body">
          <div class="img-container">
            <img :src="avatar" />
          </div>
          <div class="userInfo-container">
            <el-row>
              <el-col :span="6">用户名:</el-col>
              <el-col :span="12">{{ admin.name }}</el-col>
            </el-row>
            <el-row>
              <el-col :span="6">手机号码:</el-col>
              <el-col :span="12">{{ admin.phone }}</el-col>
            </el-row>
            <el-row>
              <el-col :span="6">电话号码:</el-col>
              <el-col :span="12">{{ admin.telephone }}</el-col>
            </el-row>
            <el-row>
              <el-col :span="6">地址:</el-col>
              <el-col :span="12">{{ admin.address }}</el-col>
            </el-row>
            <el-row>
              <el-col :span="6">用户状态:</el-col>
              <el-col :span="12">
                <el-switch style="display: block" v-model="admin.enabled" active-color="#13ce66" inactive-color="#ff4949" active-text="启用" inactive-text="未启用" @change="enabledChange(admin)"> </el-switch>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="6">用户角色:</el-col>
              <el-col :span="12">
                <template v-for="(role, index) in admin.roles">
                  <el-tag style="margin-right: 4px" type="success" :key="role.id" v-show="index == 0 || index == 1">{{ role.nameZh }}</el-tag>
                </template>
                <el-popover placement="right" title="角色列表" width="200" trigger="click" @show="showPop(admin)" @hide="hidePop(admin)">
                  <el-select v-model="selectedRoles" multiple placeholder="请选择">
                    <el-option v-for="r in allRoles" :key="r.id" :label="r.nameZh" :value="r.id"> </el-option>
                  </el-select>
                  <el-button slot="reference" type="text" icon="el-icon-more"></el-button>
                </el-popover>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="6">备注:</el-col>
              <el-col :span="12">
                {{ admin.remark }}
              </el-col>
            </el-row>
          </div>
        </div>
      </el-card>
    </div>
  </div>
</template>

<script>
export default {
  name: 'SysAdmin',
  created() {
    this.initAdmins()
  },
  data() {
    return {
      avatar: require('@/assets/images/logo1.jpg'),
      admins: [],
      //搜索关键词
      keywords: '',
      //所有角色
      allRoles: [],
      //当前admin所拥有的角色
      selectedRoles: [],
    }
  },
  methods: {
    doSearch() {
      this.initAdmins()
    },
    async initAdmins() {
      const data = await this.getRequest('/system/admin/?keywords=' + this.keywords)
      if (data) {
        this.admins = data
      }
    },
    deleteAdmin(admin) {
      this.$confirm('此操作将永久删除' + admin.name + '操作员, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          this.deleteRequest('/system/admin/' + admin.id).then((resp) => {
            if (resp) {
              this.initAdmins()
            }
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除',
          })
        })
    },
    //更新
    enabledChange(admin) {
      this.putRequest('/system/admin/', admin).then((resp) => {
        if (resp) {
          this.initAdmins()
        }
      })
    },
    showPop(admin) {
      //获取所有角色
      this.initAllRoles()
      this.selectedRoles = []
      //获取当前admin所拥有的角色
      admin.roles.forEach((role) => {
        this.selectedRoles.push(role.id)
      })
    },
    hidePop(admin) {
      let roles = [] //存放admin原有角色列表
      Object.assign(roles, admin.roles)
      let flag = false
      //如果原有admin的角色个数 跟 selectedRoles的角色个数不一样 说明修改了
      if (roles.length != this.selectedRoles.length) {
        flag = true
      } else {
        // 数量一样 但有可能角色不一样 也有可能被修改
        for (let i = 0; i < roles.length; i++) {
          let role = roles[i]
          for (let j = 0; j < this.selectedRoles.length; j++) {
            let sr = this.selectedRoles[j]
            //如果存在相同的角色id 就从roles中删掉
            if (role.id == sr) {
              roles.splice(i, 1)
              i--
              break
            }
          }
        }
        //删完发现roles的长度仍然不为0  说明肯定修改了
        if (roles.length != 0) {
          flag = true
        }
      }

      if (flag) {
        let url = '/system/admin/role?adminId=' + admin.id
        this.selectedRoles.forEach((sr) => {
          url += '&rids=' + sr
        })
        this.putRequest(url).then((resp) => {
          if (resp) {
            this.initAdmins()
          }
        })
      }
    },
    initAllRoles() {
      this.getRequest('/system/admin/roles').then((resp) => {
        if (resp) {
          this.allRoles = resp
        }
      })
    },
  },
}
</script>

<style lang="less" scoped>
.sysadmin-header {
  display: flex;
  justify-content: center;
}
.admin-container {
  margin-top: 10px;
  display: flex;
  justify-content: space-around;
  flex-wrap: wrap;
  .admin-card {
    width: 30%;
    margin-bottom: 10px;
  }
  .img-container {
    display: flex;
    justify-content: center;
    img {
      width: 70px;
      height: 70px;
      border-radius: 70px;
    }
  }
  .userInfo-container {
    display: flex;
    justify-content: center;
    flex-wrap: wrap;
    color: #409eff;
    .el-row {
      margin-top: 8px;
      display: flex;
      // flex: 1;
      width: 100%;
      justify-content: center;
      // text-align: center;
      .el-col {
        &:first-child {
          text-align: center;
        }
        &:last-child {
          padding-left: 5px;
        }
      }
    }
  }
}
</style>
